/**
 * created by 小卷毛, 2020/12/9
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.lib_tools_database.factory;

import android.text.TextUtils;

import com.sgcc.pda.lib_tools_database.bean.ticket.TicketDetailPlan;
import com.sgcc.pda.lib_tools_database.gen.DaoSession;
import com.sgcc.pda.lib_tools_database.gen.TicketDetailPlanDao;

import org.greenrobot.greendao.Property;

import java.util.List;

/**
 * 描 述：离线工作计划操作类
 * 作 者：小卷毛  2020-12-09 17:27
 * 修改描述：增加参数字段
 * 修 改 人：
 * 修改版本：v1.2.1
 */
public class TicketDetailPlanImpl extends Abstractable<TicketDetailPlan, TicketDetailPlanDao> {

    @Override
    public TicketDetailPlanDao getDao(DaoSession session) {
        return session.getTicketDetailPlanDao();
    }

    /**
     * 删除多条记录
     */
    public void delete(List<TicketDetailPlan> list) {
        mCommonDaoUtils.delete(list);
    }

    /**
     * 根据工单号、业务应用、工单类型、来源获取工作票ID
     */
    public String findIdByAppNo(
            String appNo,
            String appCode,
            String workTypeNo,
            String eomOrderSrc
    ) {
        if (TextUtils.isEmpty(appNo)) return "";

        // 查询本地数据库
        List<TicketDetailPlan> items = findByParam(
                new Property[]{TicketDetailPlanDao.Properties.OLD_APP_NO},
                new String[]{appNo});

        switch (items.size()) {
            case 0: //未查到数据
                return "";
            case 1: //查到一条数据
                return items.get(0).getPARENT_ID();
            default: //查到多条数据
                for (TicketDetailPlan item : items) {
                    if (appCode.equals(item.getAPP_CODE())
                            && workTypeNo.equals(item.getWORK_TYPE_NO())
                            && eomOrderSrc.equals(item.getWORK_FROM())) {
                        return item.getPARENT_ID();
                    }
                }
                return "";
        }
    }

    /**
     * 根据工单号、业务应用、工单类型、来源获取工作票计划
     */
    public TicketDetailPlan findDataByAppNo(
            String appNo,
            String appCode,
            String workTypeNo,
            String eomOrderSrc
    ) {
        if (TextUtils.isEmpty(appNo)) return null;

        // 查询本地数据库
        List<TicketDetailPlan> items = findByParam(
                new Property[]{TicketDetailPlanDao.Properties.OLD_APP_NO},
                new String[]{appNo});

        switch (items.size()) {
            case 0: //未查到数据
                return null;
            case 1: //查到一条数据
                return items.get(0);
            default: //查到多条数据
                for (TicketDetailPlan item : items) {
                    if (appCode.equals(item.getAPP_CODE())
                            && workTypeNo.equals(item.getWORK_TYPE_NO())
                            && eomOrderSrc.equals(item.getWORK_FROM())) {
                        return item;
                    }
                }
                return null;
        }
    }

    /**
     * 查询本地所有关联计划数量
     *
     * @param parentId 父ID
     */
    public int queryCount(String parentId) {
        return (int) mCommonDaoUtils.queryCount(
                TicketDetailPlanDao.Properties.PARENT_ID, parentId);
    }

    /**
     * 查询本地关联已结束计划数量
     *
     * @param parentId 父ID
     */
    public int queryFinishCount(String parentId) {
        return (int) mCommonDaoUtils.queryCount(
                new Property[]{
                        TicketDetailPlanDao.Properties.PARENT_ID,
                        TicketDetailPlanDao.Properties.PLAN_STATE
                },
                new String[]{parentId, "03"}
        );
    }

}
